package kr.saeildamil.androidstudy.ch17;

import kr.saeildamil.androiddforstudy.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Common extends Activity {

	final static int ADD = 0;
	final static int SUB = 1;
	final static int MUL = 2;

	EditText l_Value;
	EditText r_Value;
	EditText oper_Value;
	EditText result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch17_common);

		l_Value = (EditText) findViewById(R.id.ch17_common_left);
		oper_Value = (EditText) findViewById(R.id.ch17_common_oper);
		r_Value = (EditText) findViewById(R.id.ch17_common_right);
		result = (EditText) findViewById(R.id.ch17_common_results);

	}

	public void mOnClick(View v) {
		if (v.getId() == R.id.ch17_common_btn) {
			Intent intent = new Intent();
			intent.putExtra("l_Value", l_Value.getText().toString());
			String opr = oper_Value.getText().toString().trim();
			intent.putExtra("oper", opr);
			intent.putExtra("r_Value", r_Value.getText().toString());

			if (opr.equals("+")) {
				intent.setClass(this, AddAct.class);
				startActivityForResult(intent, ADD);
			} else if (opr.equals("-")) {
				intent.setClass(this, SubAct.class);
				startActivityForResult(intent, SUB);
			} else {
				intent.setClass(this, MulAct.class);
				startActivityForResult(intent, MUL);
			}
		}
	} // end of method mOnClick;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case ADD:
			if (resultCode == RESULT_OK) {
				Toast.makeText(this, "Add Completed", Toast.LENGTH_SHORT)
						.show();
				result.setText(data.getIntExtra("AddResult", 0) + "");
			} else {
				Toast.makeText(this, "Add Failed", Toast.LENGTH_SHORT).show();
			}
			break;

		case SUB:
			if (resultCode == RESULT_OK) {
				Toast.makeText(this, "Sub Completed", Toast.LENGTH_SHORT)
						.show();
				result.setText(data.getIntExtra("SubResult", 0) + "");
			} else {
				Toast.makeText(this, "Sub Failed", Toast.LENGTH_SHORT).show();
			}
			break;

		case MUL:
			if (resultCode == RESULT_OK) {
				Toast.makeText(this, "Mul Completed", Toast.LENGTH_SHORT)
						.show();
				result.setText(data.getIntExtra("MulResult", 0) + "");
			} else {
				Toast.makeText(this, "Mul Failed", Toast.LENGTH_SHORT).show();
			}
			break;

		}
	}

}
